package com.admin.action;

import com.admin.service.SoftWareClassifyService;
import com.data.bean.Menu;
import com.data.util.CheckDataUtils;
import com.data.util.FinalStringUtils;
import com.data.util.ImageLookUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 软件分类管理
 *
 * @author:HingLo
 * @create 2017-08-25 10:40
 **/
@Controller
@RequestMapping("/softWareClassifyAction/admin")
public class SoftWareClassifyAction {

    @Autowired
    private SoftWareClassifyService softWareClassifyService;


    /**
     * 进入到添加一级菜单分类界面
     *
     * @return
     */
    @RequestMapping("addclass")
    public String inputOneMenuAction(String params, HttpServletRequest request) {
        List<Menu> menuList = softWareClassifyService.findMenuOneService();
        request.setAttribute("dataMap", menuList);
        if (params != null) {
            request.setAttribute("bool", params);
        }

        return "/WEB-INF/admin/add_class";
    }

    /**
     * 根据分类查找菜单集合
     *
     * @param param
     * @return
     */
    @RequestMapping("menu")
    @ResponseBody
    public List<?> menuListAction(Integer param) {
        //默认为软件
        if (param == 0 || param == 1) {
            return softWareClassifyService.findMenuOneService(param);
        }
        return null;

    }


    /**
     * 添加分类
     *
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("submitAddclass")
    public String addMenuAction(@RequestParam("file") MultipartFile file, Menu menu, HttpServletRequest request) throws IOException {
        boolean bool = softWareClassifyService.addClassService(menu, file);
        request.setAttribute("bool", bool);
        return bool ? "redirect:addclass?params=" + bool : "404";
    }

    /**
     * 查看全部分类信息
     *
     * @param request
     * @return
     */
    @RequestMapping("lookClass")
    public String lookClassAction(HttpServletRequest request) {
        List<Menu> list = softWareClassifyService.lookClassService();
        request.setAttribute("dataMap", list);
        System.out.println(list.size());
        return "/WEB-INF/admin/look_class";
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping("lookOneClass")
    public String lookOneClassAction(String param, HttpServletRequest request) {
        Menu menu = null;
        if (CheckDataUtils.stringUtils(param)) {
            menu = softWareClassifyService.lookOneClassService(param);
            //查询类别
            int type = menu.getType();
            String fid = menu.getFid();
            if (String.valueOf(FinalStringUtils.SOFTTYPE).equals(fid) || String.valueOf(FinalStringUtils.GAMETYPE).equals(fid)) {

            } else {
                List<?> menuList = softWareClassifyService.findMenuOneService(type);
                request.setAttribute("dataMap", menuList);
            }
        }
        if (menu != null) {
            request.setAttribute("menu", menu);
        }
        return "/WEB-INF/admin/look_OneClass";
    }

    /**
     * @param menu
     * @param file
     * @return
     */
    @RequestMapping("changeClass")
    public String changeClassAction(@RequestParam("file") MultipartFile file, Menu menu, HttpServletRequest request) {
        boolean bool = softWareClassifyService.addClassService(menu, file);
        request.setAttribute("bool", bool);
        System.out.println("success...");
        return bool ? "/WEB-INF/admin/look_OneClass" : "404";
    }

    @RequestMapping("classIcon")
    public void classIconAction(String id, HttpServletResponse response) {
        byte[] inputStream = softWareClassifyService.lookOneClassService(id).getIcon();
        ImageLookUtils.responseImage(inputStream, response);

    }

}
